import React from 'react';
import PropTypes from 'prop-types';
import { Layout, Icon } from 'antd';
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
import { Route, Redirect, Switch } from 'dva/router';
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
import { enquireScreen } from 'enquire-js';
import GlobalHeader from '../components/GlobalHeader';
import GlobalFooter from '../components/GlobalFooter';
import SiderMenu from '../components/SiderMenu';
import NotFound from '../routes/Exception/404';
import { getRoutes } from '../common/utils/utils';


const { Content } = Layout;
const query = {
  'screen-xs': {
    maxWidth: 575,
  },
  'screen-sm': {
    minWidth: 576,
    maxWidth: 767,
  },
  'screen-md': {
    minWidth: 768,
    maxWidth: 991,
  },
  'screen-lg': {
    minWidth: 992,
    maxWidth: 1199,
  },
  'screen-xl': {
    minWidth: 1200,
  },
};

let isMobile;
enquireScreen((b) => {
  isMobile = b;
});

class BasicLayout extends React.PureComponent {
  static childContextTypes = {
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
  }

  state = {
    isMobile,
  };

  getChildContext() {
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
  }
  componentDidMount() {
    enquireScreen((b) => {
      this.setState({
        isMobile: !!b,
      });
    });
  }
  getPageTitle() {
    const { routerData, location } = this.props;
    const { pathname } = location;
    let title = 'BasePlat';
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - BasePlat`;
    }
    return title;
  }
  render() {
    const {
      collapsed, location, dispatch, match,routerData//,currentUser
    } = this.props;
    const layout = (
      <Layout>
        <SiderMenu
          collapsed={collapsed}
          location={location}
          dispatch={dispatch}
          isMobile={false}
        />
        <Layout>
          <GlobalHeader
            //currentUser={currentUser}
            dispatch={dispatch}
            collapsed={collapsed}
            isMobile={this.state.isMobile}
          />
            <Content style={{ margin: '24px 24px 0', height: '100%' }}>
               <div style={{ minHeight: 'calc(100vh - 260px)' }}>
                <Switch>
                  {
                    getRoutes(match.path, routerData).map(item => (
                      <Route
                        key={item.key}
                        path={item.path}
                        component={item.component}
                        exact={item.exact}
                      />
                    ))
                  }
                  <Redirect exact from="/" to="/system/menu" />
                  <Route render={NotFound} />
                </Switch>
              </div> 
            <GlobalFooter
              // links={[{
              //   title: 'Pro 首页',
              //   href: 'http://pro.ant.design',
              //   blankTarget: true,
              // }, {
              //   title: 'GitHub',
              //   href: 'https://github.com/ant-design/ant-design-pro',
              //   blankTarget: true,
              // }, {
              //   title: 'Ant Design',
              //   href: 'http://ant.design',
              //   blankTarget: true,
              // }]}
              copyright={
                <div>
                  Copyright <Icon type="copyright" /> 2017 ming快速开发平台
                </div>
              }
            />
          </Content>
        </Layout>
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
          {params => <div className={classNames(params)}>{layout}</div>}
        </ContainerQuery>
      </DocumentTitle>
    );
  }
}

export default connect(state => ({
  //currentUser: state.global.currentUser,
  collapsed: state.global.collapsed,
}))(BasicLayout);
